﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Solution.Data;
using Solution.Data;
using Solution.Business.Core;
using Solution.Utilities.Security;

namespace Solution.Business
{

    // news
    public class TagBLL : abstractCommonDAL<Tag>
    {
    }
    public class CategoryBLL : abstractCommonDAL<Category>
    {
    }
    public class ArticleCommentBLL : abstractCommonDAL<ArticleComment>
    {
    }
    public class ArticleBLL : abstractCommonDAL<Article>
    {
        public Article GetOne(string Alias)
        {
            try
            {
                return DB.Articles.Where(p => p.flag != "D").Where(t => t.Alias == Alias).First();
            }
            catch { return null; }
        }
    }
    public class ArticleTagBLL : abstractCommonDAL<ArticleTag>
    {
    }

    //system
    public class ConfigurationtBLL : abstractCommonDAL<Configuration>
    {
    }
    public class UserBLL : abstractCommonDAL<User>
    {
        public User GetOne(string userName)
        {
            try
            {
                return DB.Users.Where(t => t.LoginName.Equals(userName)).First();
            }
            catch { return null; }

        }
        public int Login(string userName, string passWord)
        {
            try
            {
                var _User = DB.Users.Where(t => t.LoginName.Equals(userName)).First(); ;
                if (_User.Password.Equals(Security.GetMd5Hash(passWord)))
                {
                    if (_User.flag.ToLower() == "d".ToLower())
                        return 1;
                    if (_User.flag.ToLower() == "l".ToLower())
                        return 2;
                    return 3;
                }
                else
                    return 0;
            }
            catch { return 0; }

        }
        public bool IsTrueAuthenFun(int userId, string Module, string Fun)
        {
            try
            {
                Fun = Fun.ToUpper().Trim();
                Fun = Fun.Replace(" ", "");
                string[] ArrFun = Fun.Split(',');
                Array.Sort(ArrFun); // keep A first
                /* select * from [User] a 
                 inner join [GroupMember] b on a.Id = b.UserId 
                 inner join [Group] c on b.GroupId = c.Id 
                 inner join [Policy] d on c.Id = d.GroupId 
                 inner join [Module] e on d.ModuleId = e.Id
                 where e.Alias ='system'*/
                var authen = from a in DB.Users
                             join b in DB.GroupMembers on a.Id equals b.UserId
                             join c in DB.Groups on b.GroupId equals c.Id
                             join d in DB.Policies on c.Id equals d.GroupId
                             join e in DB.Modules on d.ModuleId equals e.Id
                             where e.Alias == Module
                             select new { a.Id, d.View, d.Insert, d.Delete, d.Update, d.All };


                bool fag = false;
                foreach (var item in ArrFun)
                {
                    switch (item)
                    {
                        case "A":
                            foreach (var A in authen) if (A.All) return true;
                            break;

                        case "V":
                            fag = false;
                            foreach (var V in authen)
                            {
                                if (V.All)
                                    return true;
                                if (V.View) fag = true;
                            }
                            if (!fag) return false;
                            break;

                        case "I":
                            fag = false;
                            foreach (var I in authen)
                            {
                                if (I.All)
                                    return true;
                                if (I.Insert) fag = true;
                            }
                            if (!fag) return false;
                            break;

                        case "D":
                            fag = false;
                            foreach (var D in authen)
                            {
                                if (D.All)
                                    return true;
                                if (D.Delete) fag = true;
                            }
                            if (!fag) return false;
                            break;

                        case "U":
                            fag = false;
                            foreach (var U in authen)
                            {
                                if (U.All)
                                    return true;
                                if (U.Update) fag = true;
                            }
                            if (!fag) return false;
                            break;

                        default: break;
                    }
                }

                return true;
            }
            catch { return false; }
        }
    }
    public class GroupBLL : abstractCommonDAL<Group>
    {
    }
    public class GroupMemberBLL : abstractCommonDAL<GroupMember>
    {
    }
    public class MenuBLL : abstractCommonDAL<Menu>
    {
    }
    public class ModuletBLL : abstractCommonDAL<Module>
    {
    }
    public class PolicyBLL : abstractCommonDAL<Policy>
    {
    }


}
